package com.hh.TreeQuestions;

/**
 * @author hanghao
 * @date 2023/8/3 16:31
 * 二叉搜索树的最近公共祖先
 * middle
 * 突破口: 找出在[p,q]范围内的点就可以了
 */
public class No235 {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        while (true) {
            if (root.val > p.val && root.val > q.val) {
                root = root.left;
            } else if (root.val < p.val && root.val < q.val) {
                root = root.right;
            } else {
                break;
            }
        }
        return root;
    }
}
